﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ClientNetwork
{
    public enum SaunaProtocol
    {
        CreateUser,
        Login,
        Logout,
        GetProfile,
        GetUser,
        UpdateProfile,
        DeleteUser,
        DeleteFile,
        AddFile,
        OpenChat,
        CloseChat,
        GetUserList,
        GetFile
    };

    public class Protocol  
    {
        public static String Command(SaunaProtocol protocol, String message)
        {
            Byte[] data = new Byte[256];
            String responseData;

            Object[] returnData = new Object[2];

            // Translate the passed message into Unicode and store it as a byte array using protocol delimiters $protocol:messageSize@message
            data = System.Text.Encoding.Unicode.GetBytes("$SAU" + SaunaProtocol(protocol) + "," + message);

            // Send data to Network and return data and bytes
            returnData = Network.Connect(data);

            // Buffer to store the response bytes.
            data = new Byte[256];

            // Read the first batch of the TcpServer response bytes.
            Int32 bytes = (Int32)returnData[0];
            data = (Byte[])returnData[1];
            responseData = System.Text.Encoding.Unicode.GetString(data, 0, bytes);

            return responseData;
        }

        private static String SaunaProtocol(SaunaProtocol protocol)
        {
            switch(protocol.ToString())
            {
                case "CreateUser":
                    return "CU";
                    break;

                case "Login":
                    return "LI";
                    break;

                case "Logout":
                    return "LO";
                    break;

                case "GetProfile":
                    return "GP";
                    break;

                case "GetUser":
                    return "GU";
                    break;

                case "UpdateProfile":
                    return "UP";
                    break;


                case "DeleteUser":
                    return "DU";
                    break;

                case "DeleteFile":
                    return "DF";
                    break;


                case "AddFile":
                    return "AF";
                    break;

                case "OpenChat":
                    return "OC";
                    break;

                case "CloseChat":
                    return "CC";
                    break;

                case "GetUserList":
                    return "GUL";
                    break;

                case "GetFile":
                    return "GF";
                    break;

                default:
                    return "";
                    break;
            }
        }
    }
}
